package config

import (
	"github.com/go-ini/ini"
	"log"
)

var (
	Cfg             *ini.File
	RunMode         string
	RunHost         string
	USERNAME        string
	PASSWORD        string
	HOST            string
	PORT            string
	DATABASE        string
	CHARSET         string
	EndPoint        string
	AccessKeyID     string
	SecretAccessKey string
	UseSSL          bool
)

func init() {
	var err error
	Cfg, err = ini.Load("config/app.ini")
	if err != nil {
		log.Fatalf("Fail to parse 'config/app.ini': %v", err)
	}
	LoadBase()
	LoadServer()
	LoadSQL()
	LoadMinIO()
}

func LoadBase() {
	RunMode = Cfg.Section("").Key("RUN_MODE").MustString("debug")
}

func LoadServer() {
	sec, err := Cfg.GetSection("server")
	if err != nil {
		log.Fatalf("Fail to get section 'server': %v", err)
	}

	RunHost = sec.Key("RUN_HOST").MustString(":8000")
}

func LoadSQL() {
	sec, err := Cfg.GetSection("mysql")
	if err != nil {
		log.Fatalf("Fail to get section 'app': %v", err)
	}
	USERNAME = sec.Key("USERNAME").MustString("root")
	PASSWORD = sec.Key("PASSWORD").MustString("")
	HOST = sec.Key("HOST").MustString("127.0.0.1")
	PORT = sec.Key("PORT").MustString("3306")
	DATABASE = sec.Key("DATABASE").MustString("")
	CHARSET = sec.Key("CHARSET").MustString("")
}
func LoadMinIO() {
	sec, err := Cfg.GetSection("minio")
	if err != nil {
		log.Fatalf("Fail to get section 'app': %v", err)
	}
	EndPoint = sec.Key("EndPoint").MustString("")
	AccessKeyID = sec.Key("AccessKeyID").MustString("")
	SecretAccessKey = sec.Key("SecretAccessKey").MustString("")
	UseSSL = sec.Key("UseSSL").MustBool(true)
}
